package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.Customer;
import com.woniuxy.java106crm.entity.CustomerLinkman;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.CustomerLinkmanService;
import com.woniuxy.java106crm.service.CustomerService;
import com.woniuxy.java106crm.util.RandomUtils;
import com.woniuxy.java106crm.vo.CustomerVo;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Api(tags = "客户管理模块API文档")
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerLinkmanService customerLinkmanService;

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("查询所有客户")
    //获取所有正常状态的客户信息
    @GetMapping("/findAllCustomers/{page}/{size}")
    public ResponseResult<ResPage<List<Customer>>> findAllCustomers(
            @PathVariable("page")Integer page, @PathVariable("size") Integer size){
        ResPage<List<Customer>> resPage = customerService.findAllCustomers(page,size);
        return new ResponseResult<>(ResponseResult.OK,resPage);
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("编辑客户信息")
    //编辑客户信息
    @PostMapping("/updateCustomer")
    public ResponseResult<Boolean> updateCustomer(@RequestBody Customer customer){
        return new ResponseResult<>(ResponseResult.OK,customerService.updateCustomer(customer));
    }

    /**
     * 增加客户信息
     */
    @PreAuthorize("hasAuthority('客户开发计划')")
    @RequireMethod("增加客户信息")
    @PostMapping("/addCustomer")
    public ResponseResult addCustomer(@RequestBody CustomerVo customerVo){
        String kh = RandomUtils.createRandomStringUseTime("KH");
        customerVo.setId(kh);
        CustomerLinkman customerLinkman = new CustomerLinkman();
        customerLinkman.setCid(kh);
        customerLinkman.setPhone(customerVo.getPhone());
        customerLinkmanService.addLinkman(customerLinkman);
        return new ResponseResult(ResponseResult.OK, customerService.addCustomer(customerVo));
    }

    @RequireMethod("条件查询客户")
    //条件查询客户信息
    @GetMapping("/queryCondition/{page}/{size}")
    public ResponseResult<ResPage<List<Customer>>> queryCondition(
            @PathVariable("page")Integer page, @PathVariable("size") Integer size, Customer customer){
        System.out.println(customer);
        ResPage<List<Customer>> resPage = customerService.queryCondition(page,size,customer);
        return new ResponseResult<>(ResponseResult.OK,resPage);
    }

}
